﻿using App.Common;
using App.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace App.Controllers
{
    public class RegistController : BaseController
    {
        private readonly IusersService _user;
        public RegistController(IusersService user)
        {
            this._user = user;
        }
        //
        // GET: /Regist/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(RegisterViewModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                //判断邮箱
                var user = _user.GetAsync(s => s.Email.Equals(viewmodel.Email));
                if (user != null)
                {
                    System.Resources.ResourceManager rs = new System.Resources.ResourceManager("App.ErrorMessages", typeof(ErrorMessages).Assembly);
                    var title = rs.GetString("EmailExistence");
                    ModelState.AddModelError("Email", title);
                }
                else {
                    String newError = ValidatePwd.Valid(viewmodel.Password);
                    if (!string.IsNullOrEmpty(newError))
                    {
                        System.Resources.ResourceManager rs = new System.Resources.ResourceManager("App.ErrorMessages", typeof(ErrorMessages).Assembly);
                        var title = rs.GetString("PasswordError");
                        ModelState.AddModelError("Password", title);
                        return View();
                    }
                    else{
                        users member = new users();
                        member.UserName = Globals.HtmlEncode(string.IsNullOrEmpty(viewmodel.UserName) ? Guid.NewGuid().ToString() : viewmodel.UserName);
                        member.Email = viewmodel.Email;
                        member.CreateTime = System.DateTime.Now;
                        member.State = (int)CommonState.正常;
                        var passwordSalt = Common.SecurityHelper.CreateSalt();
                        var password = Cryptographer.EncodePassword(viewmodel.Password, 1, passwordSalt);
                        member.EncryptContent = passwordSalt;
                        member.Pwd = password;
                        member.RegistIP = ControllerContext.RequestContext.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                        member.type = 0;
                        member.FirstName = viewmodel.FirstName;
                        member.LastName = viewmodel.LastName;
                        var model = _user.Add(member);
                        if (model.Id > 0)
                            return RedirectToAction("Index", "Login");
                        else
                        {
                            return View();
                        }
                    }
                        
                    

                }
                
            }
            return View();
        }

       
       
	}
}